<?php
/**
 * Created by PhpStorm.
 * User: 户学
 * Date: 2018/4/18
 * Time: 13:52
 */

class User_model extends CI_Model
{
    /***
     * 注册
    */
    public function register($data){
//        $mobile = $data['mobile'];
        //先查询该手机号码是否已经注册
        $count = $this->db->where(array('mobile'=>$data['mobile']))->get("user")->result_array();
        if(count($count)>0){
           return -1;
        }
        $result = $this->db->insert('user',$data);
        return $result;
    }
    /**
     * 登录
    **/
    public function login($phone, $password, $expire, $back = 0, $is_uid = 0){
        // 查询会员信息
        $data = array();
        if(!preg_match("/^1[34578]{1}\d{9}$/",$phone)){
            return -3;
        }
        if ($is_uid) {
            $data = $this->db->where('id', (int)$phone)->get('user')->row_array();
            $phone = $data['mobile'];
        } else {
            $data = $this->db->where('mobile', $phone)->get('user')->row_array();
        }
//        echo $password."<br>";
//        echo md5($password)."<br>";
//        echo $data['pass'];exit;
        if(empty($data)){
            return -1;
        }else{
            if(md5($password) == $data['pass']){
                set_cookie('user_id',$data['id'],86500);
                set_cookie("name",$data['name'],86500);
                set_cookie("mobile",$data['mobile'],86500);
                //登陆成功
                return 1;
            }else if(md5($password) != $data['pass']){
                return -2;
            }else{
                //登陆失败
                return -4;
            }
        }
    }

    /**
     * 退出登陆
     */
    public function logout(){
        $synlogin = '<script type="text/javascript" src="'.HOME_THEME_PATH.'/index.php?s=user&c=login&m=index"></script>';
        return $synlogin;
    }

    public function get_user($id){
        $uid = intval($id);
        if (!$uid) {
            return NULL;
        }

        // 查询会员信息
        $db = $this->db
            ->from($this->db->dbprefix('user'))
            ->where('id', $uid)
            ->limit(1)
            ->get();
        if (!$db) {
            return NULL;
        }
        $data = $db->row_array();
        if (!$data) {
            return NULL;
        }
        return $data;
    }



    /**
     * 添加一条通知
     *
     * @param	string	$uid
     * @param	intval	$type 1系统，2互动，3模块，4应用
     * @param	string	$note
     * @return	null
     */
    public function add_notice($uid, $type, $note) {

        print_r($uid);
        print_r($type);
        print_r($note);
        exit;
        if (!$uid || !$note) {
            return NULL;
        }

        $uids = is_array($uid) ? $uid : explode(',', $uid);
        foreach ($uids as $uid) {
            $tableid = (int)substr((string)$uid, -1, 1);
            $this->db->insert('member_notice_'.$tableid, array(
                'uid' => $uid,
                'type' => $type,
                'isnew' => 1,
                'content' => $note,
                'inputtime' => SYS_TIME,
            ));
            $this->db->replace('member_new_notice', array('uid' => $uid));
        }

        return NULL;
    }

}